"""
Frame for input.

This frame display automatically the corresponding frame
depending on the selected radio button.
By default, TextInputFrame is displayed.
"""

import tkinter as tk
from ui.DataProvidingFrame import DataProvidingFrame
from ui.BinaryInputFrame import *
from ui.FileChoiceFrame import *
from ui.TextInputFrame import *
from ui.HexaInputFrame import *

class CrcInputFrame(tk.Frame):
    """This frame contains the input choices for the data to CRC.

    Its contents change dynamically when a new input choice is
    choosen."""
    def update(self):
        """Change the frame depending on the input choice."""

        if self.selectionFrameExist:
            self.selectionFrame.grid_remove()
        else:
            self.selectionFrameExist = True

        self.selectionFrame = self.MODES[self.radio_var.get()]
        self.selectionFrame.grid(column=0, row=len(self.MODES), sticky=tk.N+tk.S+tk.W)

        # Disabled verbose mode for file.
        if self.radio_var.get() == 'File':
            self.verboseModeAllowed = 0;
        else:
            self.verboseModeAllowed = 1;
        # Force the GUI to be updating.
        self.parent.parent.menubar.update_verbose_mode()

    def create_widgets(self):
        """Create the frame starting layout."""
        # The frames are already constructed, the update code only changes
        # the one displayed.
        self.defaultItem = 'String'
        self.MODES = {
            'String':CrcTextInputFrame(self,(self.width, self.height)),
            'Binary':CrcBinaryInputFrame(self,(self.width, self.height)),
            'File':CrcFileChoiceFrame(self,(self.width, self.height)),
            'Hexa':CrcHexaInputFrame(self,(self.width, self.height)),
            }
        assert len(self.MODES), "There must be a least one input method."
        self.create_input_method_choices()
        #self.create_help_button()
    
    def create_input_method_choices(self):
        """Create the radio buttons."""
        # The variable object must persist.
        self.radio_var = tk.StringVar()
        self.radio_var.set(self.defaultItem)
        
        # Create radio buttons and select the first one.
        i = 0
        for mode in self.MODES:
            b = tk.Radiobutton(self, text=mode, variable=self.radio_var,
                               value=mode, command=self.update)
            b.grid(column=0, row=i, sticky=tk.W)
            if mode == self.defaultItem:
                b.select()
            i += 1
    
    def __init__(self,master=None):
        tk.Frame.__init__(self, master)
        self.parent = master
        self.width = 350
        self.height = 200
        self["width"] = self.width
        self["height"] = self.height
        self["relief"] = "sunken"
        self["borderwidth"] = "2"

        self.selectionFrameExist = False
        self.create_widgets()
        
    def getDataGenerator(self):
        """Override from DataProvidingFrame.
        Return a generator (or None) for the data."""
        if self.selectionFrameExist:
            return self.selectionFrame.getDataGenerator()
        else:
            return None

    def getDataSize(self):
        """Override from DataProvidingFrame.
        Return the size of the data."""
        if self.selectionFrameExist:
            return self.selectionFrame.getDataSize()
        else:
            return None
